<template>
  <div class="club-list">
    <h1 class="page-title">俱乐部管理</h1>

    <!-- 搜索和筛选 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline>
        <el-form-item label="关键词">
          <el-input
            v-model="searchForm.keyword"
            placeholder="搜索俱乐部名称"
            clearable
          />
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="全部" clearable>
            <el-option label="待审核" value="pending" />
            <el-option label="已通过" value="approved" />
            <el-option label="已拒绝" value="rejected" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            <span>搜索</span>
          </el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 俱乐部列表 -->
    <el-card class="table-card">
      <el-table
        :data="clubList"
        v-loading="loading"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        
        <el-table-column label="俱乐部信息" min-width="250">
          <template #default="{ row }">
            <div class="club-info">
              <el-avatar :src="row.avatar || '/default-club.png'" :size="50" />
              <div class="club-detail">
                <div class="name">{{ row.name }}</div>
                <div class="description">{{ row.description || '暂无描述' }}</div>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="成员数" width="120">
          <template #default="{ row }">
            {{ row.currentMembers || 0 }} / {{ row.maxMembers || 0 }}
          </template>
        </el-table-column>

        <el-table-column prop="level" label="等级" width="100">
          <template #default="{ row }">
            <el-tag type="success">Lv.{{ row.level || 1 }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="points" label="积分" width="100" />

        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="createdAt" label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createdAt) }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" size="small" @click="viewDetail(row.id)">
              查看
            </el-button>
            <el-button 
              v-if="row.status === 'pending'" 
              link 
              type="success" 
              size="small" 
              @click="approveClub(row.id)"
            >
              通过
            </el-button>
            <el-button 
              v-if="row.status === 'pending'" 
              link 
              type="danger" 
              size="small" 
              @click="rejectClub(row.id)"
            >
              拒绝
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.page_size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="loadClubs"
        @current-change="loadClubs"
        class="pagination"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { clubApi } from '@/api/club'
import dayjs from 'dayjs'

const router = useRouter()

const loading = ref(false)
const clubList = ref([])

const searchForm = reactive({
  keyword: '',
  status: ''
})

const pagination = reactive({
  page: 1,
  page_size: 20,
  total: 0
})

onMounted(() => {
  loadClubs()
})

const loadClubs = async () => {
  loading.value = true

  try {
    const params = {
      page: pagination.page,
      page_size: pagination.page_size,
      ...searchForm
    }

    const data = await clubApi.getClubList(params)
    
    clubList.value = data.items || []
    pagination.total = data.total || 0
  } catch (error) {
    console.error('加载俱乐部列表失败:', error)
    ElMessage.error('加载俱乐部列表失败')
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  pagination.page = 1
  loadClubs()
}

const handleReset = () => {
  searchForm.keyword = ''
  searchForm.status = ''
  handleSearch()
}

const viewDetail = (id) => {
  router.push(`/clubs/${id}`)
}

const approveClub = (id) => {
  ElMessageBox.confirm('确定要通过该俱乐部的审核吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'success'
  }).then(async () => {
    try {
      await clubApi.approveClub(id)
      ElMessage.success('审核通过')
      loadClubs()
    } catch (error) {
      console.error('审核失败:', error)
    }
  }).catch(() => {})
}

const rejectClub = (id) => {
  ElMessageBox.prompt('请输入拒绝原因', '拒绝审核', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputType: 'textarea',
    inputPlaceholder: '请输入拒绝原因',
    inputValidator: (value) => {
      if (!value) {
        return '请输入拒绝原因'
      }
      return true
    }
  }).then(async ({ value }) => {
    try {
      await clubApi.rejectClub(id, value)
      ElMessage.success('已拒绝')
      loadClubs()
    } catch (error) {
      console.error('操作失败:', error)
    }
  }).catch(() => {})
}

const getStatusType = (status) => {
  const types = {
    pending: 'warning',
    approved: 'success',
    rejected: 'danger',
    active: 'success'
  }
  return types[status] || ''
}

const getStatusText = (status) => {
  const texts = {
    pending: '待审核',
    approved: '已通过',
    rejected: '已拒绝',
    active: '活跃'
  }
  return texts[status] || status
}

const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}
</script>

<style scoped>
.club-list {
  padding: 20px;
}

.page-title {
  font-size: 24px;
  margin-bottom: 20px;
  color: #303133;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  border-radius: 8px;
}

.club-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.club-detail {
  flex: 1;
  min-width: 0;
}

.name {
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.description {
  font-size: 12px;
  color: #909399;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>

